<?php
namespace Amoysea\Controller;
use Think\Controller;
class RegController extends Controller {
    public function login(){
        $this->display();
    }
    public function verify(){
		ob_clean();
        $config =    array(
            'fontSize'    =>    12,
            'length'      =>    3,
            'useNoise'    =>    false,
            'bg'=>array(229,229,229),
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }
    public function verify_check($code,$id){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
    public function loginCheck($username,$password,$type,$company,$verify){
        if($type!=0 && $type!=1){
            alert(0,'操作有误');
        }
        if(!$this->verify_check($verify)){
            alert(0,'验证码错误');
        }
        if($username==''){
            alert(0,'用户名不能为空');
        }
        if(!mobile($username) && $type==1){
            alert(0,'手机号码错误');
        }
        if($password==''){
            alert(0,'密码不能为空');
        }
        if($type==0 && $company==''){
            alert(0,'企业名称不能为空');
        }
        $where['username']=$username;
        if($type==1){//主账号
            $result=M('user')->where($where)->find();
			if($result['status']==0){
				alert(0,'正在审核！');
			}
			if($result['status']==2){
				alert(0,'未通过审核！');
			}
            if(count($result)==0){
                alert(0,'用户名不存在');
            }
            if($result['password']==md5($password. $result['salt'])){
                $result['db']=C('DB_PREFIX_T').$result['username'];
                $result['type']=1;
                $result['company_id']=$result['id'];
                session('company',$result);
                alert(1,'登录成功');
            }else{
                alert(0,'密码错误');
            }
        }else{///次账号
            $user=M('user')->where(array('company'=>$company))->find();
			$user['db']=C('DB_PREFIX_T').$user['username'];
            $m_username=$user['username'];
            if($m_username===null){
                alert(0,'公司名称不存在');
            }
            $db_name=C('DB_PREFIX_T').$m_username;
            $this->product = M('role',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
            $result=$this->product->where($where)->find();
			$department_id=$result['department_id'];
			$department_info=M('Department',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name)->where(array('id'=>$department_id))->find();
			$result['price_level']=$department_info['price_level'];
            if(count($result)==0){
                alert(0,'用户名不存在');
            }
            if($result['password']==md5($password. $result['salt'])){
                $result['db']=C('DB_PREFIX_T').$m_username;
                $result['type']=0;
                $result['company_id']=$user['id'];
				$user['type']=0;
                $result['company']=$user['company'];
                session('company',$user);
				session('company.childs',$result);//子账号
                $this->product->where($where)->setInc("login_count");
                $a=$this->product->where($where)->setField("last_login_date",date("Y-m-d H:i:s"),time());
                alert(1,'登录成功');
            }else{
                alert(0,'密码错误');
            }
        }
    }
    public function register(){
		// dump(session());
        $this->display();
    }
	//渠道终端注册
	public function reg(){
        $this->display();
    }
    public function exits(){
        session('company',null);
        session('user_root',null);
        $this->redirect('Reg/login');
    }
	public function regD($company,$username,$pwd,$pwd2,$name,$email,$code){
		$u=M("user");
        if($company==''){
            alert(0,'企业名称不能为空');
        }
        if($username==''){
            alert(0,'手机号不能为空');
        }
		if($code==''){
            alert(0,'邀请码不能为空');
        }
        if($pwd==''){
            alert(0,'密码不能为空');
        }
		if(strlen($pwd)<6){
            alert(0,'密码至少六位');
        }
        if($pwd2!=$pwd){
            alert(0,'两次密码不相同');
        }
        if($email==''){
            alert(0,'E-mail不能为空');
        }
        if($name==''){
            alert(0,'姓名不能为空');
        }
        if(!email($email)){
            alert(0,'邮箱地址错误');
        }
        if(!mobile($username)){
            alert(0,'手机号码错误');
        }
		$user_info=$u->where("company='".$company."'")->find();
		if(empty($user_info)){
			alert(0,'没有查到此企业信息');
		}else{
			$user_username=$user_info['username'];
			$db_name=C('DB_PREFIX_T').$user_username;
			$m=M('Role', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$in=M('Invitation', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			//参数验证
			if($m->where(array('username'=>$username))->count()>0){
				alert(0,'手机号已经注册');
			}
			if($m->where(array('email'=>$email))->count()>0){
				alert(0,'邮箱已经注册');
			}
			if($in->where(array('code'=>$code,'status'=>1))->count()==0){
				alert(0,'邀请码无效');
			}
			// $member=I('member');
			$data['salt']=rand(10000,99999);
			$data['username']=$username;
			// $data['member']=$member;
			$data['code']=$code;
			$data['password']=md5($pwd.$data['salt']);
			$data['name']=$name;
			$data['email']=$email;
			$data['status']=0;
			$result=$m->add($data);
			if($result){
				$in->where(array('code'=>$code))->setInc('nums');//邀请码使用次数加1
				alert(1,'注册成功');
			}else{
				alert(0,'注册失败，请重试');
			}
		}
    }
    public function registerD($company,$username,$pwd,$pwd2,$name,$email){

        if($company==''){
            alert(0,'用户名不能为空');
        }
        if($username==''){
            alert(0,'用户名不能为空');
        }
        if($pwd==''){
            alert(0,'密码不能为空');
        }
        if($pwd2!=$pwd){
            alert(0,'两次密码不相同');
        }
        if($email==''){
            alert(0,'E-mail不能为空');
        }
        if($name==''){
            alert(0,'姓名不能为空');
        }
        if(!email($email)){
            alert(0,'邮箱地址错误');
        }
        if(!mobile($username)){
            alert(0,'手机号码错误');
        }
        if(M("user")->where("username='".$username."'")->count()>=1){
            alert(0,'用户名已经存在！');
        }
        if(M("user")->where("company='".$company."'")->count()>=1){
            alert(0,'公司名称已经存在！');
        }
        if(M("user")->where("email='".$email."'")->count()>=1){
            alert(0,'电子邮箱已经存在！');
        }
		// $member=I('member');
        $data['salt']=rand(10000,99999);
        $data['username']=$username;
		// $data['member']=$member;
        $data['company']=$company;
        $data['password']=md5($pwd.$data['salt']);
        $data['name']=$name;
        $data['email']=$email;
		$data['status']=0;
        $result=M("user")->add($data);
        if($result){
			$reg=A('Company/Reg');
            if($reg->createDb(C('DB_PREFIX_T').$username)){
                alert(1,'注册成功');
            }else{
                alert(0,'注册失败，请重试');
            }
        }else{
            alert(0,'注册失败，请重试');
        }
    }
}